package qsc.algorithm.sort.insertSort;

import java.util.Arrays;

/**
 * @auther QiuShangcheng
 * @create 2021/5/20
 *
 *  插入排序
 * 参考链接：https://blog.csdn.net/oneby1314/article/details/107847693
 */
public class InsertSort {
    public static void main(String[] args) {
        int[] arr = { 101, 34, 119, 1 };
        int[] res = insertSort(arr);
        System.out.println(Arrays.toString(res));
    }

    public static int[] insertSort(int[] arr){
        int insertVal;
        int insertIndex;
        for (int i = 1; i < arr.length; i++) {
            insertIndex = i-1;
            insertVal = arr[i];
            while (insertIndex >=0 && insertVal < arr[insertIndex]){
                arr[insertIndex+1] = arr[insertIndex];
                insertIndex--;
            }
            arr[insertIndex+1] = insertVal;
        }
        return arr;
    }
}
